package com.ruoyi.user.utils;

import com.alibaba.fastjson.JSON;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 张桦阳
 * @Date: 2023-02-23-16:11:22
 * @Description: 类
 */

public class BankUtil {
    //String card_number,String id_number,String name,String phone_number
    public static boolean checkBank(String bankcard,String idcard,String rolename,String mobile) {
        String host = "https://dfbank234.market.alicloudapi.com";
        String path = "/verify_id_name_bankcard_phone";
        String method = "POST";
        String appcode = "57cc4ad7cebe413b8ffaabdbd42c9af9";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("card_number", bankcard);
        bodys.put("id_number", idcard);
        bodys.put("name", rolename);
        bodys.put("phone_number", mobile);

        String bankCard = null;
        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            //{"status":"OK","state":1,"result_code":8300,"result_message":"核验成功","request_id":"TID00138ba027fd42109da34ebd229352d2"}
            //{"status":"INVALID_ARGUMENT","reason":"Invalid id number","request_id":"TIDb18e7082b9e34269843a4252e2c4c747"}
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            bankCard = EntityUtils.toString(response.getEntity());
        } catch (Exception e) {
            e.printStackTrace();
        }

        Map map = JSON.parseObject(bankCard, Map.class);
        String status = (String) map.get("status");
        if (status.equals("OK")){
            return true;
        }else{
            return false;
        }
    }
}
